package com.leadbank.baselbf.outils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Admin on 2017/9/26.
 * 时间管理类
 */
public class MyTimeUtil {
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static String yyyy = "yyyy";
    public static String yyyy_MM_dd = "yyyy-MM-dd";
    public static String yyyyMMdd = "yyyyMMdd";
    public static String yyyyMM = "yyyyMM";
    public static String yyyy_MM = "yyyy-MM";
    public static String HH_mm = "HH:mm";
    public static String MM_dd = "MM-dd";
    public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
    public static String yyyy_MM_dd_HH = "yyyy-MM-dd HH";

    public static String yyyyMMddHHmm = "yyyyMMddHHmm";
    public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";



    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime(String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
    /**
     * 获取今天的日期
     *
     * @return
     */
    public static Date getNowDate() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }
    /**
     * 获取2个字符日期的天数差
     *
     * @param p_startDate
     * @param p_endDate
     * @return 天数差
     */
    public static long getDaysOfTowDiffDate(String p_startDate, String p_endDate) {

        Date l_startDate = MyTimeUtil.strToDate(MyTimeUtil.yyyy_MM_dd, p_startDate);
        Date l_endDate = MyTimeUtil.strToDate(MyTimeUtil.yyyy_MM_dd, p_endDate);
        long l_startTime = l_startDate.getTime();
        long l_endTime = l_endDate.getTime();
//        long betweenDays = (long) ((l_endTime - l_startTime) / (1000 * 60 * 60 * 24));
        long betweenDays = (long) ((l_endTime - l_startTime) / (1000));
        return betweenDays;
    }

    /**
     * 获取去年今天的日期
     *
     * @param formatStr
     * @return
     */
    public static Date getLstYearDate(String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1); //年份减1
        return c.getTime();
    }

    /**
     * 将字符串时间改成Date类型
     *
     * @param format
     * @param dateStr
     * @return
     */
    public static Date strToDate(String format, String dateStr) {

        Date date = null;

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }


    /**
     * 将Date时间转成字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String DateToStr(String format, Date date) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        return simpleDateFormat.format(date);
    }



    /**
     * 将一种类型的日期转换为另一种，如：
     *
     * yyyyMMddHHmm 转化为 yyyy-MM-dd HH:mm
     * @param format
     * @param formDate
     * @param toFormat
     * @return
     */
    public static String dateStrChange(String format, String formDate,String toFormat) {

        Date date = null;

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            date = simpleDateFormat.parse(formDate);

            SimpleDateFormat toSimpleDateFormat = new SimpleDateFormat(toFormat);
            return toSimpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }


}
